package com.hm.jiaowuxitong.ui;

import com.hm.jiaowuxitong.service.AdminServiceImp;
import com.hm.jiaowuxitong.service.IAdminService;
import com.hm.jiaowuxitong.service.IStudentService;
import com.hm.jiaowuxitong.service.StudentServiceImp;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChangeAdminPassword {
    public static void main(String[] args) {
        new ChangeAdminPassword();
    }
    public ChangeAdminPassword(){
        JFrame frame=new JFrame("修改密码");
        frame.setSize(800,470);
        //创建画布
        JPanel panel = new JPanel(new BorderLayout());
        //创建方向布局
        SpringLayout layout = new SpringLayout();
        panel.setLayout(layout);

        //创建文本框控件
        JLabel label5 = new JLabel("账户名称:");
        label5.setFont(new Font(" ", 3, 20));
        label5.setForeground(Color.black);
        layout.putConstraint(SpringLayout.WEST, label5, 180, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, label5, 60, SpringLayout.NORTH, panel);

        JTextField jtf1=new JTextField(16);
        jtf1.setFont(new Font(" ", 3, 15));
        jtf1.setForeground(Color.black);
        layout.putConstraint(SpringLayout.WEST, jtf1, 5, SpringLayout.EAST, label5);
        layout.putConstraint(SpringLayout.NORTH, jtf1, 62, SpringLayout.NORTH, panel);
        //创建文本框控件
        JLabel label1 = new JLabel("旧密码:");
        label1.setFont(new Font(" ", 3, 20));
        label1.setForeground(Color.black);
        layout.putConstraint(SpringLayout.WEST, label1, 200, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, label1, 100, SpringLayout.NORTH, panel);

        JLabel label2 = new JLabel("新密码:");
        layout.putConstraint(SpringLayout.WEST, label2, 200, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, label2, 15, SpringLayout.SOUTH, label1);
        label2.setForeground(Color.black);
        label2.setFont(new Font(" ", 3, 20));

        JLabel label3 = new JLabel("确认新密码:");
        layout.putConstraint(SpringLayout.WEST, label3, 160, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, label3, 15, SpringLayout.SOUTH, label2);
        label3.setForeground(Color.black);
        label3.setFont(new Font(" ", 3, 20));

        //创建登录按钮
        JButton button1 = new JButton("确认修改");
        layout.putConstraint(SpringLayout.WEST, button1, 295, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, button1, 300, SpringLayout.NORTH, panel);
        button1.setFont(new Font(" ", 3, 15));
        JButton button2 = new JButton("返回");
        layout.putConstraint(SpringLayout.WEST, button2, 20, SpringLayout.EAST, button1);
        layout.putConstraint(SpringLayout.NORTH, button2, 300, SpringLayout.NORTH, panel);
        button2.setFont(new Font(" ", 3, 15));

        //密码显示
        JPasswordField passwordField = new JPasswordField(16);
        JButton toggleButton = new JButton("👁");
        layout.putConstraint(SpringLayout.WEST, toggleButton, 184, SpringLayout.WEST, passwordField);
        layout.putConstraint(SpringLayout.NORTH, toggleButton, 102, SpringLayout.NORTH, panel);
        toggleButton.setFont(new Font(" ", 3, 13));

        layout.putConstraint(SpringLayout.WEST, passwordField, 5, SpringLayout.EAST, label1);
        layout.putConstraint(SpringLayout.NORTH, passwordField, 105, SpringLayout.NORTH, panel);
        passwordField.setFont(new Font(" ", 3, 15));

        JPasswordField passwordField1 = new JPasswordField(16);
        JButton toggleButton1 = new JButton("👁");
        layout.putConstraint(SpringLayout.WEST, toggleButton1, 184, SpringLayout.WEST, passwordField1);
        layout.putConstraint(SpringLayout.NORTH, toggleButton1, 140, SpringLayout.NORTH, panel);
        toggleButton1.setFont(new Font(" ", 3, 13));

        layout.putConstraint(SpringLayout.WEST, passwordField1, 5, SpringLayout.EAST, label2);
        layout.putConstraint(SpringLayout.NORTH, passwordField1, 15, SpringLayout.SOUTH, passwordField);
        passwordField1.setFont(new Font(" ", 3, 15));

        //密码显示
        JPasswordField passwordField2 = new JPasswordField(16);
        JButton toggleButton2 = new JButton("👁");
        layout.putConstraint(SpringLayout.WEST, toggleButton2, 184, SpringLayout.WEST, passwordField2);
        layout.putConstraint(SpringLayout.NORTH, toggleButton2, 180, SpringLayout.NORTH, panel);
        toggleButton2.setFont(new Font(" ", 3, 13));

        layout.putConstraint(SpringLayout.WEST, passwordField2, 5, SpringLayout.EAST, label3);
        layout.putConstraint(SpringLayout.NORTH, passwordField2, 15, SpringLayout.SOUTH, passwordField1);
        passwordField2.setFont(new Font(" ", 3, 15));

        toggleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (passwordField.getEchoChar() == 0) {
                    passwordField.setEchoChar('*');
                } else {
                    passwordField.setEchoChar((char) 0);
                }
            }
        });
        toggleButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (passwordField1.getEchoChar() == 0) {
                    passwordField1.setEchoChar('*');
                } else {
                    passwordField1.setEchoChar((char) 0);
                }
            }
        });
        toggleButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (passwordField2.getEchoChar() == 0) {
                    passwordField2.setEchoChar('*');
                } else {
                    passwordField2.setEchoChar((char) 0);
                }
            }
        });
        toggleButton.setBorderPainted(false);//不显示按钮的边框。
        toggleButton.setRolloverEnabled(true);//启用按钮悬停效果。
        toggleButton1.setBorderPainted(false);//不显示按钮的边框。
        toggleButton1.setRolloverEnabled(true);//启用按钮悬停效果。
        toggleButton2.setBorderPainted(false);//不显示按钮的边框。
        toggleButton2.setRolloverEnabled(true);//启用按钮悬停效果。
        toggleButton.setBorderPainted(false);//不显示按钮的边框。
        toggleButton.setRolloverEnabled(true);//启用按钮悬停效果。
        toggleButton.setForeground(Color.black);
        toggleButton.setContentAreaFilled(false);
        toggleButton.setFocusPainted(false);
        toggleButton.setOpaque(false);
        toggleButton.setFocusPainted(false);
        toggleButton1.setBorderPainted(false);//不显示按钮的边框。
        toggleButton1.setRolloverEnabled(true);//启用按钮悬停效果。
        toggleButton1.setForeground(Color.black);
        toggleButton1.setContentAreaFilled(false);
        toggleButton1.setFocusPainted(false);
        toggleButton1.setOpaque(false);
        toggleButton1.setFocusPainted(false);
        toggleButton2.setBorderPainted(false);//不显示按钮的边框。
        toggleButton2.setRolloverEnabled(true);//启用按钮悬停效果。
        toggleButton2.setForeground(Color.black);
        toggleButton2.setContentAreaFilled(false);
        toggleButton2.setFocusPainted(false);
        toggleButton2.setOpaque(false);
        toggleButton2.setFocusPainted(false);

        panel.add(label1);
        panel.add(label2);
        panel.add(label3);
        panel.add(label5);
        panel.add(jtf1);
        panel.add(toggleButton);
        panel.add(passwordField);
        panel.add(toggleButton1);
        panel.add(passwordField1);
        panel.add(toggleButton2);
        panel.add(passwordField2);
        panel.add(button1);
        panel.add(button2);


        //把画布添加至面板
        frame.setContentPane(panel);
        //禁止面板放大缩小
        frame.setResizable(false);
        //允许关闭整个应用
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //将面板显示
        frame.setVisible(true);
        // 设置窗口居中显示
        frame.setLocationRelativeTo(null);
        // 显示窗口
        frame.setVisible(true);
        button1.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {

                IAdminService service = new AdminServiceImp();
                List<Map<String, Object>> map = service.HgetUser(jtf1.getText());
                if (map.size()!=0) {
                    if (map.get(0).get("key").equals(passwordField.getText())) {
                        if (passwordField.getText().length() == 0 || passwordField1.getText().length() == 0) {
                            JOptionPane.showMessageDialog(null, "密码或新密码不能为空", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                        } else if (passwordField2.getText().length() == 0) {
                            JOptionPane.showMessageDialog(null, "请再次确认新密码", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                        } else if (!passwordField1.getText().equals(passwordField2.getText())) {
                            JOptionPane.showMessageDialog(null, "输入的密码需相同", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                        }
                        else if (passwordField.getText().equals(passwordField1.getText())) {
                            JOptionPane.showMessageDialog(null, "旧密码与新密码不能相同", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                        }else {
                            //把多个数据封装成一个对象，便于传输
                            String key=passwordField1.getText();
                            String id=jtf1.getText();
                            Map<String,Object> oMap=new HashMap<>();
                            oMap.put("id",id);
                            oMap.put("key",key);
                            //把封装好的数据传递给业务逻辑层接口进行加工
                            int i=service.ChangeAdminPassword(oMap);
                            if (i>0){
                                //学生信息已添加数据库成功
                                JOptionPane.showMessageDialog(null, "修改成功", "消息提示", JOptionPane.WARNING_MESSAGE);//消息对话框
                                //释放内存
                                frame.dispose();
                            }

                        }
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "旧密码错误", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    }
                }

                else {
                    JOptionPane.showMessageDialog(null, "获取用户信息失败", "消息提示", JOptionPane.WARNING_MESSAGE);
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }


}
